var post = require('./posts');

// app/routes.js
module.exports = function(app, passport) {
    // =====================================
    // HOME PAGE (with login links) ========
    // =====================================
    app.get('/', function(req, res) {
        res.render('index.ejs'); // load the index.ejs file
    });

    // =====================================
    // LOGIN ===============================
    // =====================================
    // show the login form
    app.get('/login', function(req, res) {
        // render the page and pass in any flash data if it exists
        res.render('login.ejs', { postdbmessage: req.flash('loginMessage') });
    });

    // process the login form
    app.post('/login', passport.authenticate('local-login', {
        successRedirect : '/', // redirect to the secure profile section
        failureRedirect : '/login', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

    app.post('/authentication', function(req, res, next) {
        passport.authenticate('local-login', function(err, user, info) {
            if (err) {
                return next(err);
            }

            if (!user) {
                //return res.redirect('/login');
                res.statusCode = 401;
                return res.send({
                    ErrorCode: 401,
                    ErrorMessage: "Bad login/password"
                });
            }
            req.logIn(user, function(err) {
                if (err) {
                    return next(err);
                }
                return res.send(user);
            });
        })(req, res, next);
    });


    // =====================================
    // SIGNUP ==============================
    // =====================================
    // show the signup form
    app.get('/signup', function(req, res) {

        // render the page and pass in any flash data if it exists
        res.render('signup.ejs', { message: req.flash('signupMessage') });

        //res.render('signup.ejs');
    });

    // process the signup form
    app.post('/signup', passport.authenticate('local-signup', {
        successRedirect : '/profile', // redirect to the secure profile section
        failureRedirect : '/signup', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

    // =====================================
    // PROFILE SECTION =====================
    // =====================================
    // we will want this protected so you have to be logged in to visit
    // we will use route middleware to verify this (the isLoggedIn function)
    app.get('/profile', isLoggedIn, function(req, res) {
        res.render('profile.ejs', {
            user : req.user // get the user out of session and pass to template
        });
    });

    // =====================================
    // LOGOUT ==============================
    // =====================================
    app.get('/logout', function(req, res) {
        req.logout();
        res.redirect('/');
    });


    // =====================================
    // POSTS ==============================
    // =====================================
    app.get('/posts', post.findAll);
    app.get('/posts/:id', isLoggedIn, post.findById);
    app.post('/posts', post.findSearchedPost);
    //app.get('/posts', isLoggedIn, post.findAll);
/*    app.get('/posts/:id', isLoggedIn, post.findById);
    app.post('/posts', isLoggedIn, post.addPost);
    app.put('/posts/:id', isLoggedIn, post.updatePost);
    app.delete('/posts/:id', isLoggedIn, post.deletePost);*/
};

// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {
    console.log('Check if user authenticated');


    console.log('req: ' + req);
    console.log('Isauthenticated?: ' + req.isAuthenticated());

    // if user is authenticated in the session, carry on
    if (req.isAuthenticated())
    {
        console.log('User authenticated');
        return next();
    }

    // if they aren't redirect them to the home page
    res.statusCode = 401;
    return res.send({
            ErrorCode: 401,
            ErrorMessage: "Unauthorized"
        });

    //res.redirect('/');
}
